﻿using Datav.Design.Datas.Pocos;
using Datav.Design.IService;
using WorldDesign.Datas;
using Zdap.Common;
using Zdap.Common.AutoMapper;
using Zdap.Common.Extensions;
using Zdap.XCode.Service;

namespace Datav.Design.Service
{
    /// <summary>
    ///
    /// </summary>
    public class ProjectGroupService : BaseService<ProjectGroup, ProjectGroupModel, GroupDto>, IProjectGroupService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public override async Task<ListResponseContext<GroupDto>> SearchAll(RequestParameter p)
        {
            var entitys = await base.SearchAll(p);

            var groupIds = entitys.Data.Select(s => s.Id);

            var projects = await Project.FindAllAsync(Project._.GroupId.In(groupIds) & Project._.IsDelete == 0);

            entitys.Data.ForEach(s =>
            {
                s.Children = projects.Select(s => s.MapTo<ProjectModel>()).Where(w => w.GroupId == s.Id).ToList();
            });

            return await Task.FromResult(entitys);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async ValueTask<ResponseContext<bool>> GroupName(long id, string name, UserTicket user)
        {
            var rest = new ResponseContext<bool> { };
            var s = await ProjectGroup.FindAsync(ProjectGroup._.Id == id & ProjectGroup._.IsDelete == 0);
            s.Name = name;
            rest.Data = s.Update() > 0 ? true : false;
            rest.SetSuccess("修改成功！");
            return await Task.FromResult(rest);
        }

    }
}